package com.kindel.controller.book;

import com.common.model.enums.ErrorCode;
import com.common.model.response.JsonResponse;
import com.kindel.model.book.BookModel;
import com.kindel.service.book.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by winte on 2017/8/24.
 */
@Controller
@RequestMapping("kindel/admin")
@EnableAutoConfiguration
@Api(value = "图书接口", description = "")
public class BookController {
    private Logger logger = Logger.getLogger(BookController.class);
    @Autowired
    private BookService bookService;

    @ResponseBody
    @ApiOperation(value = "根据书名，作者，类目进行查询，搜索图书功能", notes = "ErrorCode 为success 即为成功")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "书名", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "author", value = "作者", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "typeName", value = "类目", required = false)
    })
    @RequestMapping(value = "loadBook",method = RequestMethod.POST)
    public JsonResponse<Map<String, Object>> loadBook(String name, String author, String typeName){
        List<BookModel> list=new ArrayList<>();
        list=bookService.loadBook(name,author,typeName);


        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("list",list);
        return new JsonResponse<Map<String, Object>>(ErrorCode.SUCCESS,resultMap);
    }

    @ResponseBody
    @ApiOperation(value = "查询所有的图书，不需要传递参数")
    @RequestMapping(value = "loadBookAll",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "session_3rd", value = "session_3rd", required = true)
    })
    public JsonResponse<Map<String, Object>> loadBookAll(String session_3rd){
        List<BookModel> list=new ArrayList<>();
        list=bookService.loadBookAll();

        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("list",list);
        resultMap.put("session_3rd",session_3rd);
//        return new JsonResponse<Map<String, Object>>(ErrorCode.FAILED_KINDLECP_SESSION_3RD_EXPIRE,resultMap);
        return new JsonResponse<Map<String, Object>>(ErrorCode.SUCCESS,resultMap);
    }


    //一周热推
    @RequestMapping(value = "loadBookWeek",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "一周热推")
    public List<BookModel> loadBookWeek(){
        Random  r=new Random();
        int count=r.nextInt(6)+5;
        List<BookModel> list=new ArrayList<>();
        list=bookService.loadBookWeek(count);
        return list;
    }

    //今日随机
    @RequestMapping(value = "loadBookRandom",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "今日随选，不需要传递参数")
    public List<BookModel> loadBookRandom(){
        Random  r=new Random();
        int count=r.nextInt(6)+5;
        List<BookModel> list=new ArrayList<>();
        list=bookService.loadBookRandom(count);
        return list;
    }
}
